<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>IO Utility</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>IO Utility</h1>

    
        <a href="#toc" class="jump">Jump to Table of Contents</a>
    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro">
<p>
    The IO family of modules provide a simple API for requesting resources over HTTP and HTTPS.
</p>
</div>

<h2 id="getting-started">Getting Started</h2>

<p>
To include the source files for IO Utility and its dependencies, first load
the YUI seed file if you haven't already loaded it.
</p>

<pre class="code prettyprint">&lt;script src=&quot;http:&#x2F;&#x2F;yui.yahooapis.com&#x2F;3.4.1&#x2F;build&#x2F;yui&#x2F;yui-min.js&quot;&gt;&lt;&#x2F;script&gt;</pre>


<p>
Next, create a new YUI instance for your application and populate it with the
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
YUI will automatically load any dependencies required by the modules you
specify.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create a new YUI instance and populate it with the required modules.
YUI().use(&#x27;io&#x27;, function (Y) {
    &#x2F;&#x2F; IO Utility is available and ready for use. Add implementation
    &#x2F;&#x2F; code here.
});</pre>


<p>
For more information on creating YUI instances and on the
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
documentation for the <a href="../yui/index.html">YUI Global object</a>.
</p>


<h2 id="a-simple-transaction">A Simple Transaction</h2>
<p>
The <code>io()</code> method on the YUI instance is the static method for making an HTTP request.  This method accepts two arguments: the uri and the configuration object.
</p>
<ul>
    <li><strong>uri:</strong> This parameter specifies the URI for the transaction</li>
    <li><strong>configuration:</strong> This parameter is an object of keys and values of configurations specific to the transaction.  Please see: <a href="#the-configuration-object">The Configuration Object</a> for more information on all available configuration properties.</li>
</ul>
<p>
<code>io()</code> returns an object with the following members:
</p>
<ul>
    <li><strong>id:</strong> This is the unique identifier of the transaction.</li>
    <li><strong>abort():</strong> Aborts the specific transaction.</li>
    <li><strong>isInProgress():</strong> Returns a boolean value indicating whether the transaction has completed.</li>
</ul>
<p>
This is an example GET transaction, handling the response at the earliest opportunity.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create a YUI instance using io-base module.
YUI().use(&quot;io-base&quot;, function(Y) {
    var uri = &quot;get.php?foo=bar&quot;;

    &#x2F;&#x2F; Define a function to handle the response data.
    function complete(id, o, args) {
        var id = id; &#x2F;&#x2F; Transaction ID.
        var data = o.responseText; &#x2F;&#x2F; Response data.
        var args = args[1]; &#x2F;&#x2F; &#x27;ipsum&#x27;.
    };

    &#x2F;&#x2F; Subscribe to event &quot;io:complete&quot;, and pass an array
    &#x2F;&#x2F; as an argument to the event handler &quot;complete&quot;, since
    &#x2F;&#x2F; &quot;complete&quot; is global.   At this point in the transaction
    &#x2F;&#x2F; lifecycle, success or failure is not yet known.
    Y.on(&#x27;io:complete&#x27;, complete, Y, [&#x27;lorem&#x27;, &#x27;ipsum&#x27;]);

    &#x2F;&#x2F; Make an HTTP request to &#x27;get.php&#x27;.
    &#x2F;&#x2F; NOTE: This transaction does not use a configuration object.
    var request = Y.io(uri);
});</pre>



<h2 id="using-io">Using IO</h2>

<h3 id="the-io-modules">The IO modules</h3>
<p>IO's core and supplementary features are split into several modules, to allow greater flexibility in selecting the desired features.</p>
 <table>
    <thead>
        <tr><th>Module</th><th>Description</th></tr>
    </thead>
    <tbody>
        <tr>
            <td><code>io-base</code></td>
            <td>This is the base module, containing the basic functionality needed for making HTTP requests.</td>
        </tr>
        <tr>
            <td><code>io-xdr</code></td>
            <td>This module extends io-base, to add cross-domain request capabilities using alternate HTTP transports.</td>
        </tr>
        <tr>

            <td><code>io-form</code></td>
            <td>This module extends io-base, to add the ability to serialize HTML form data for POST transactions.</td>
        </tr>
        <tr>
            <td><code>io-upload-iframe</code></td>
            <td>This module extends io-base, to allow file uploads with an HTML form, using an iframe as the transaction transport.</td>
        </tr>

        <tr>
            <td><code>io-queue</code></td>
            <td>This module extends io-base, to provide a basic queue interface for io.</td>
        </tr>
    </tbody>
</table>


<h3 id="the-configuration-object">The Configuration Object</h3>

<p>This object is the second argument of <code>io()</code>.  Its properties define transaction parameters and transaction response handling.  The configuration object and all configuration properties are optional.  The following table describes all configuration properties used by IO.</p>

<table>
    <thead>
        <tr>
            <th>Property</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>method (string)</td>
            <td>Defines the HTTP method for the transaction.  If this property is undefined or omitted, the default value is GET.</td>
        </tr>
        <tr>
            <td>data (string)</td>
            <td>Data to be sent with the transaction, defined as a string of key-value pairs(e.g., <code>"foo=bar&baz=boo"</code>.)  Data can also be defined as a single-level object(e.g., { 'foo':'bar', 'baz':'boo' }), which is then serialized into a key-value string.  To use this capability, the sub-module <code>querystring-stringify-simple</code>, an optional dependency for io-base, must be declared in <code>Y.use()</code>.</td>
        </tr>
        <tr>
            <td>headers (object)</td>
            <td>Specific HTTP headers and values to be sent with the transaction (e.g., <code>{ 'Content-Type': 'application/xml; charset=utf-8' } </code>).</td>
        </tr>
        <tr>
            <td>on (object)</td>
            <td>This object can be used to register transaction event handlers for the set of supported io events, listed below. These events fire in addition to the global io events.  All events handlers are optional.
                <ul class="topspace">
                    <li><strong>start:</strong> <em>event handler</em></li>
                    <li><strong>complete:</strong> <em>event handler</em></li>
                    <li><strong>success:</strong> <em>event handler</em></li>
                    <li><strong>failure:</strong> <em>event handler</em></li>
                    <li><strong>end:</strong> <em>event handler</em></li>
                </ul>
            NOTE: These events are accessible only to the transaction's subscribers, whereas global IO events are accessible by all subscribers.
            </td>
        </tr>
        <tr>
            <td>context (object)</td>
            <td>Defines the execution context of the event handler functions for the transaction. If undefined, a default value of <code>Y(the YUI instance)</code> is used.</td>
        </tr>
        <tr>
            <td>form (object)</td>
            <td>This object instructs io to use the labels and values of the specified HTML form as data.
                <ul class="topspace">
                    <li><strong>id:</strong> This property can be defined as the id(String) of an HTML form or an object reference of the HTML form.</li>
                    <li><strong>useDisabled:</strong> When set to <strong>true</strong>, disabled field values are included as part of the data.  The default value is false.</li>
                </ul>
            </td>
        </tr>
        <tr>
            <td>xdr (object)</td>
            <td>Defines the transport to be used for cross-domain requests (e.g., <code>{ use:'flash' }</code> ).  The transaction will use the specified transport instead of the default transport, when specified in the transaction's configuration object.
                <ul class="topspace">
                    <li><strong>use:</strong> This property specifies the type of transport to be used; the io-xdr module provides 'native' and 'flash' transports.</li>
                    <li><strong>dataType:</strong> When set to <strong>'xml'</strong>, io will return the response data as an XML document, if necessary.</li>
                </ul>
            </td>
        </tr>
        <tr>
            <td>sync (boolean)</td>
            <td>When set to <code>true</code>, the transaction will be processed synchronous, and will halt all code execution until the transaction is complete.</td>
        </tr>
        <tr>
            <td>arguments (object)</td>
            <td><p>Object, array, string, or number passed to all registered, transaction event handlers.  This value is available as the second argument in the "start" and "end" event handlers; and, it is the third argument in the "complete", "success", and "failure" event handlers.</p></td>
        </tr>
        <tr>
            <td>timeout</td>
            <td>This value, defined as milliseconds, is a time threshold for the transaction (e.g., <code>{ timeout: 2000 }</code> ). When this limit is reached, and the transaction's Complete event has not yet fired, the transaction will be aborted.</td>
        </tr>
    </tbody>
</table>

<p>This is an example of a configuration object, with a set of properties defined.</p>

<pre class="code prettyprint">&#x2F;*
 * This is an example configuration object with all properties defined.
 *
 * method: This transaction will use HTTP POST.
 * data: &quot;user=yahoo&quot; is the POST data.
 * headers: Object of HTTP request headers for this transaction.  The
 *          first header defines &quot;Content-Type&quot; and the second is a
 *          custom header.
 * on: Object of defined event handlers for &quot;start&quot;, &quot;complete&quot;,
 *     and &quot;end&quot;.  These handlers are methods of an object
 *     named &quot;Dispatch&quot;.
 * context: Event handlers will execute in the proper object context,
 *          so usage &#x27;this&#x27; will reference Dispatch.
 * form: Object specifying the HTML form to be serialized into a key-value
 *       string and sent as data; and, informing io to include disabled
 *       HTML form fields as part of the data.  If input type of &quot;file&quot;
 *       is present, setting the upload property to &quot;true&quot; will create an
 *       alternate transport, to submit the HTML form with the
 *       selected files.
 * xdr: Instructs io to use the defined transport, in this case Flash,
 *      to make a cross-domain request for this transaction.
 * arguments: Object of data, passed as an argument to the event
 *            handlers.
 *&#x2F;
var cfg = {
    method: &#x27;POST&#x27;,
    data: &#x27;user=yahoo&#x27;,
    headers: {
        &#x27;Content-Type&#x27;: &#x27;application&#x2F;json&#x27;,
    },
    on: {
        start: Dispatch.start,
        complete: Dispatch.complete,
        end: Dispatch.end
    },
    context: Dispatch,
    form: {
        id: formObject,
        useDisabled: true,
        upload: true
    },
    xdr: {
        use: &#x27;flash&#x27;,
        dataType: &#x27;xml&#x27;
    },
    arguments: {
        start: &#x27;foo&#x27;,
        complete: &#x27;bar&#x27;,
        end: &#x27;baz&#x27;
    }
};</pre>


<h3 id="the-response-object">The Response Object</h3>
<p>
When a transaction, using the XHR object as a transport, is complete, the response data are passed as an object to the event handler.
</p>
<table>
    <thead>
        <tr><th>Field</th><th>Description</th></tr>
    </thead>
    <tbody>
    <tr>
      <td><strong>status</strong></td>

      <td>The HTTP status code of the transaction.</td>
    </tr>
    <tr>
      <td><strong>statusText</strong></td>
      <td>The HTTP status message.</td>
    </tr>
    <tr>

      <td><strong>getResponseHeader(<em>headername</em>)</strong></td>
      <td>Returns the string value of the specified header label.</td>
    </tr>
    <tr>
      <td><strong>getAllResponseHeaders()</strong></td>
      <td>All response HTTP headers are available as a string.  Each key-value pair is delimited by "\n".</td>

    </tr>
    <tr>
      <td><strong>responseText</strong></td>
      <td>The response data as a decoded string.</td>
    </tr>
    <tr>
      <td><strong>responseXML</strong></td>
      <td>The response data as an XML document.</td>
    </tr>
    </tbody>
  </table>

<p>
NOTE: Transactions involving file upload or cross-domain requests, using alternate transports, will only populate the <code>responseText</code> or <code>responseXML</code> field.  The HTTP status and response headers are either inaccessible or unavailable to these alternate transports.
</p>

<h3 id="events">Events</h3>
<p>
IO events provide access to state and data during a transaction's lifecycle.  There are two aspects to io events: global and transaction.  Global events are always fired by io for all transactions, and these events are accessible to all event subscribers.  Transaction events are created and fired if they have handlers defined in the configuration object.  Global events are identified by the <code>io:eventname</code> pattern.
</p>
<p>
The following table describes the available io events and provides examples of how to subscribe to them globally:
</p>
<table>
    <thead>
        <tr><th>Event</th><th>Description</th></tr>
    </thead>
    <tbody>
        <tr>
            <td><strong>io:start</strong></td>
            <td>
            <p>Fires when a request is made to a resource.  The event handler's arguments signature is:</p>

<pre class="code prettyprint">function onStart(transactionid, arguments) {
  &#x2F;&#x2F; transactionid : The transaction&#x27;s ID.
  &#x2F;&#x2F; arguments: Array [&#x27;foo&#x27;,&#x27;bar&#x27;].
}

&#x2F;&#x2F; Subscribe to &quot;io.start&quot;.
Y.on(&#x27;io:start&#x27;, onStart, Y, { start: [&#x27;foo&#x27;,&#x27;bar&#x27;]; );</pre>

            </td>
        </tr>
        <tr>
            <td><strong>io:complete</strong></td>
            <td>
            <p>Fires after "io:start", when the transaction is complete and response data are available.  This is the earliest opportunity to access any response data.  The event handler's arguments signature is:</p>
<pre class="code prettyprint">function onComplete(transactionid, response, arguments) {
  &#x2F;&#x2F; transactionid : The transaction&#x27;s ID.
  &#x2F;&#x2F; response: The response object.
  &#x2F;&#x2F; arguments: Object containing an
                array { complete: [&#x27;foo&#x27;, &#x27;bar&#x27;] }.
}

&#x2F;&#x2F; Subscribe to &quot;io.complete&quot;.
Y.on(&#x27;io:complete&#x27;, onComplete, Y, { complete: [&#x27;foo&#x27;, &#x27;bar&#x27;] } );</pre>

            </td>
        </tr>
        <tr>
            <td><strong>io:success</strong></td>
            <td>
            <p>Fires after the "complete" event, when the response HTTP status resolves to 2xx.  The event handler's arguments signature is:</p>
<pre class="code prettyprint">function onSuccess(transactionid, response, arguments) {
  &#x2F;&#x2F; transactionid : The transaction&#x27;s ID.
  &#x2F;&#x2F; response: The response object.
  &#x2F;&#x2F; arguments: Boolean value &quot;true&quot;.
}

&#x2F;&#x2F; Subscribe to &quot;io.success&quot;.
Y.on(&#x27;io:success&#x27;, onSuccess, Y, true);</pre>

            </td>
        </tr>
        <tr>
            <td><strong>io:failure</strong></td>
            <td>
            <p>Fires after the "complete" event, when the response HTTP status resolves to 4xx. 5xx, undefined, or a non-standard HTTP status.  This event also includes 'abort' and 'timeout' conditions.  The event handler's arguments signature is:</p>
<pre class="code prettyprint">function onFailure(transactionid, response, arguments) {
  &#x2F;&#x2F; transactionid : The transaction&#x27;s ID.
  &#x2F;&#x2F; response: The response object.  Only status and
  &#x2F;&#x2F;           statusText are populated when the
  &#x2F;&#x2F;           transaction is terminated due to abort
  &#x2F;&#x2F;           or timeout.  The status will read
  &#x2F;&#x2F;           0, and statusText will return &quot;timeout&quot;
  &#x2F;&#x2F;           or &quot;abort&quot; depending on the mode of
  &#x2F;&#x2F;           termination.
  &#x2F;&#x2F; arguments: String &quot;Transaction Failed&quot;.
}
&#x2F;&#x2F; Subscribe to &quot;io.failure&quot;.
Y.on(&#x27;io:failure&#x27;, onFailure, Y, &#x27;Transaction Failed&#x27;);</pre>

            </td>
        </tr>
        <tr>
            <td><strong>io:end</strong></td>
            <td>
            <p>Fires at the conclusion of a transaction, after "success" or "failure" has been determined..  The event handler's arguments signature is:</p>
<pre class="code prettyprint">function onEnd(transactionid, arguments) {
  &#x2F;&#x2F; transactionid : The transaction&#x27;s ID.
  &#x2F;&#x2F; arguments: Number 0.
}

&#x2F;&#x2F; Subscribe to &quot;io.end&quot;.
Y.on(&#x27;io:end&#x27;, onEnd, Y, 0);</pre>

            </td>
        </tr>
        <tr>
            <td><strong>io:xdrReady</strong></td>
            <td>
            Fires when the flash XDR transport is ready for use.  This event only fires once, when the transport initialization is complete.
            </td>
        </tr>
    </tbody>
</table>

<p>The following example demonstrates an IO transaction with an event handler subscribed to "io:complete".</p>
<pre class="code prettyprint">&#x2F;&#x2F; Create a YUI instance using io module.
YUI().use(&quot;io-base&quot;, function(Y) {
    &#x2F;*
     * Create a function as the event handler for the event &quot;io:complete&quot;.
     *
     * The function will receive the following arguments:
     * - The ID of the transaction
     * - Object containing the response data.
     * - Argument one defined when subscribing to the event(e.g., &quot;foo&quot;).
     * - Argument two defined when subscribing to the event(e.g., &quot;bar&quot;).
     *&#x2F;
    function onComplete(transactionId, responseObject, arg1, arg2) {
        &#x2F;*
         * The argument &#x27;responseObject&#x27; is the response object.  Its
         * properties are:
         * - status
         * - statusText
         * - getResponseHeader(headerName)
         * - getAllResponseHeaders
         * - responseText
         * - responseXML
         *
         * NOTE: In an XDR transaction, only the responseText or the responseXML property is defined.
         *&#x2F;
    };

    &#x2F;*
     * Subscribe to the event &quot;io:complete&quot;, using Y.on.
     *
     * - &#x27;io:complete&#x27; : Subscribe to this io event.
     * - onComplete : The event handler to be subscribed to &#x27;io:complete&#x27;.
     * - Y : The execution context of the event handler, in this case, the YUI sandbox.
     *       since the doComplete is defined as a global function.
     * - &#x27;foo&#x27; : The first argument received by the event handler.
     * - &#x27;bar&#x27; : The second argument received by the event handler.
     *           Additional arguments can be defined, as desired.
     *&#x2F;
    Y.on(&#x27;io:complete&#x27;, onComplete, Y, &quot;foo&quot;, &quot;bar&quot;);

    &#x2F;&#x2F; Starts the transaction.
    var request = Y.io(uri);
});</pre>


<h3 id="synchronous-transactions">Synchronous Transactions</h3>
    <p>For same-domain requests, YUI io can be instructed to send a synchronous request, which will halt all script execution until the transaction is complete.  When the transaction is complete, the response data are directly accessible through the object returned by Y.io(), and the data are also accessible through all io events.  When making synchronous requests, abort() and isInProgress() are not available.</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create a YUI instance using the io-base module.
YUI().use(&quot;io-base&quot;, function(Y) {
    var cfg,
        request;
    &#x2F;&#x2F; Create a configuration object for the synchronous transaction.
    cfg = {
        sync: true,
        arguments: { &#x27;foo&#x27; : &#x27;bar&#x27; }
    };

    &#x2F;*
     * var request will contain the following fields, when the
     * transaction is complete:
     * - id
     * - status
     * - statusText
     * - getResponseHeader()
     * - getAllResponseHeaders()
     * - responseText
     * - responseXML
     * - arguments
     *&#x2F;
    request = Y.io(uri, cfg);
});</pre>


<h3 id="cross-domain-transactions">Cross-Domain Transactions</h3>
<p>By default, <code>io</code> uses the XMLHttpRequest object as the transport for HTTP transactions.  It can also be configured to use an alternate transport to make cross-domain, HTTP transactions.  Currently, io can make use of Flash as an alternate transport.  To prepare io for Flash-based, cross-domain transactions, the transport <code>io.swf</code> must be deployed and accessible to YUI io. (The file "io.swf" can be found in YUI io's build directory in the YUI3 download at: http://yuilibrary.com/downloads/.)  For each transaction, the configuration object's <code>xdr</code> object must be defined as <code>{ use: 'flash' }</code> so io will use the designated transport instead of using the default XMLHttpRequest transport.</p>
<p>
As <code>io.swf</code> is written in ActionScript 3, Flash Player 9 or better is required (version <strong>9.0.124</strong> or better is recommended).  Additionally, a cross-domain policy file must be deployed at the resource to grant the client access to the remote domain.  A cross-domain request will not be successful without this policy file hosted at the resource.  The following example file grants permissive access to the host from all requests, but the host will only accept custom HTTP headers originating from <code>yahoo.com</code>.
</p>

<pre class="code prettyprint">&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;!DOCTYPE cross-domain-policy SYSTEM
&quot;http:&#x2F;&#x2F;www.adobe.com&#x2F;xml&#x2F;dtds&#x2F;cross-domain-policy.dtd&quot;&gt;
&lt;cross-domain-policy&gt;
    &lt;allow-access-from domain=&quot;*&quot;&#x2F;&gt;
    &lt;allow-http-request-headers-from domain=&quot;*.yahoo.com&quot; headers=&quot;*&quot;&#x2F;&gt;
&lt;&#x2F;cross-domain-policy&gt;</pre>

<p>
For more information on cross-domain policy file specifications, see the following articles at Adobe Developer Connection.
</p>
<ul>
    <li><a href="http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html">Cross-Domain Policy File Specifications</a>.</li>
    <li><a href="http://kb.adobe.com/selfservice/viewContent.do?externalId=kb403030">HTTP Headers Blacklist</a>.</li>
</ul>
<p>
The following example demonstrates a cross-domain transaction, starting with the initialization of the XDR transport and subscribing to three global io events.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create a YUI instance using the io cross-domain submodule
YUI().use(&quot;io-xdr&quot;, function(Y) {
    &#x2F;&#x2F; Create a configuration object with the src property defined,
    &#x2F;&#x2F; src: The path to &quot;io.swf&quot; relative to the HTML file.
    var xdrCfg = {
        src:&#x27;io.swf&#x27;
    };

    &#x2F;&#x2F; Initialize the cross-domain transport
    Y.io.transport(xdrCfg);

    &#x2F;&#x2F; Define the configurations to be used for each transaciton..
    var cfg = {
      xdr: { use: &#x27;flash&#x27;}, &#x2F;&#x2F; Instruct io to use the flash XDR transport.
      data: &#x27;foo=bar&amp;baz=boo&#x27;, &#x2F;&#x2F; Key-value string of data.
      timeout: 3000, &#x2F;&#x2F; Abort the transaction, if it is still pending, after 3000ms.
      &#x2F;&#x2F; An object passed, as an argument, to the event handlers.
      arguments: {
        start: &#x27;foo&#x27;,
        complete: &#x27;bar&#x27;,
        end: &#x27;baz&#x27;
      }
    };

    &#x2F;*
     * GlobalEventHandler is an example object that encapsulates
     * event handlers for &quot;io:start&quot;, &quot;io:complete&quot;, and &quot;io:end&quot;.
     *
     * start( ) &#x2F;&#x2F; Event handler for &quot;io:start&quot;
     * success( ) &#x2F;&#x2F; Event handler for &quot;io:complete&quot;.
     * end( ) &#x2F;&#x2F; Event handler for &quot;io:end&quot;.
     *&#x2F;
    var GlobalEventHandler = {
      start: function(id, args) {
        var args = args.start &#x2F;&#x2F; &#x27;foo&#x27;
      },
      success: function(id, o, args) {
        var args = args.complete; &#x2F;&#x2F; &#x27;bar&#x27;
        var data = o.responseText;
        var xml = o.responseXML;
      },
      end: function(id, args) {
        var args = args.end &#x2F;&#x2F; args = &#x27;baz&#x27;
      }
    };

    function callIo() {
        &#x2F;&#x2F;example URI.
        var uri = &quot;http:&#x2F;&#x2F;pipes.yahooapis.com&#x2F;&quot;,
            &#x2F;&#x2F; Start the transaction
            request = Y.io(uri, cfg);
    }

    &#x2F;&#x2F; Subscribe GlobalEventHandler.start to event &quot;io:start&quot;.
    Y.on(&#x27;io:start&#x27;, GlobalEventHandler.start, Y);
    &#x2F;&#x2F; Subscribe GlobalEventHandler.complete to event &quot;io:complete&quot;.
    Y.on(&#x27;io:success&#x27;, GlobalEventHandler.complete, Y);
    &#x2F;&#x2F; Subscribe GlobalEventHandler to event &quot;io:end&quot;.
    Y.on(&#x27;io:end&#x27;, GlobalEventHandler.end, Y);

    &#x2F;&#x2F; Once the Flash transport is initialized and ready for use,
    &#x2F;&#x2F; it will fire the &quot;io:xdrReady&quot; event.  Subscribe to it,
    &#x2F;&#x2F; to automatically call function &quot;callIo&quot; when the transport
    &#x2F;&#x2F; is ready..
    Y.on(&#x27;io:xdrReady&#x27;, callIo, Y);

});</pre>

<p>
Note: Cross-domain transactions do not fire the global <code>io:complete</code> event and the transaction-specific <code>complete</code> event, when using the IE XDomainRequest or the Flash transport.  All other events in the transaction lifecycle are fired.
</p>
<p>
A subset of A-grade browsers are capable of making cross-domain requests, using XMLHttpRequest, requiring specific access control headers be served from the resource.  To use this feature, the xdr configuration must be defined with: <code>{ use: 'native' }</code>.  IO will try to resolve the request using the native transport, and it will fall back to the Flash transport if the initial attempt throws an exception due to the browser lacking native support.
</p>
<p>
<strong>NOTE:</strong> For native cross-domain requests to work, the resource <strong>must</strong> respond with the "Access-Control-Allow-Origin" header with a value permitting the client to make the request.  In the absence of this HTTP response header, the transaction will always fail.  Please see the following articles for more information on this topic.
</p>
<ul>
    <li>Mozilla Developer Center: <a href="https://developer.mozilla.org/En/HTTP_Access_Control">HTTP Access Control article</a>.</li>
    <li>MSDN: <a href="http://msdn.microsoft.com/en-us/library/cc709423(VS.85).aspx">Cross-Domain Security article</a>.</li>
    <li>W3C: <a href="http://dev.w3.org/2006/waf/access-control/">Access Control Working Draft</a>,</li>
</ul>

<h3 id="serializing-html-form-as-data">Serializing HTML Form as Data</h3>
<p>
IO can serialize HTML form fields into a string of UTF-8 encoded, name-value pairs.  If the transaction is HTTP GET, the data are appended to the URI as a querystring.  If the transaction if HTTP POST, the data will be the POST message.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create a YUI instance using the io-form sub-module.
YUI().use(&quot;io-form&quot;, function(Y) {
    &#x2F;&#x2F; Create a configuration object for the file upload transaction.
    &#x2F;&#x2F; The form configuration should include two defined properties:
    &#x2F;&#x2F; id: This can be the ID or an object reference to the HTML form.
    &#x2F;&#x2F; useDisabled: Set this property to &quot;true&quot; to include disabled
    &#x2F;&#x2F;              HTML form fields, as part of the data.  By
    &#x2F;&#x2F;              default, disabled fields are excluded from the
    &#x2F;&#x2F;              serialization.
    &#x2F;&#x2F; The HTML form data are sent as a UTF-8 encoded key-value string.
    var cfg = {
        method: &#x27;POST&#x27;,
        form: {
            id: formObject,
            useDisabled: true
        }
    };

    &#x2F;&#x2F; Define a function to handle the response data.
    function complete(id, o, args) {
      var id = id; &#x2F;&#x2F; Transaction ID.
      var data = o.responseText; &#x2F;&#x2F; Response data.
      var args = args[1]; &#x2F;&#x2F; &#x27;ipsum&#x27;.
    };

    &#x2F;&#x2F; Subscribe to event &quot;io:complete&quot;, and pass an array
    &#x2F;&#x2F; as an argument to the event handler &quot;complete&quot;.
    Y.on(&#x27;io:complete&#x27;, complete, Y, { &#x27;foo&#x27;:&#x27;bar&#x27; });

    &#x2F;&#x2F; Start the transaction.
    var request = Y.io(uri, cfg);
});</pre>




<h3 id="uploading-files-in-an-html-form">Uploading Files in an HTML Form</h3>
<p>
The default XHR transport, used in IO, cannot upload HTML form data that include elements of type="file".  In this situation, IO will use an alternate transport -- an iframe -- to facilitate the transaction. The response data must be one of the following content types: "text/html", "text/plain", "text/xml". The following example shows how to configure a transaction involving file upload:
</p>

<pre class="code prettyprint">&#x2F;*
 * This example demonstrates how to configure io to upload files
 * from an HTML form.  This example uses the global events:
 * &quot;io:start&quot; and &quot;io:complete&quot; to handle the transaction and
 * response.  Transaction events can be defined and fired, as well,
 * in the configuration object; but, they are not used in this
 * example.
 *&#x2F;
&#x2F;&#x2F; Create a YUI instance using the io-upload-iframe sub-module.
YUI().use(&quot;io-upload-iframe&quot;, function(Y) {
    &#x2F;&#x2F; Create a configuration object for the file upload transaction.
    &#x2F;&#x2F; The form configuration should include two defined properties:
    &#x2F;&#x2F; id: This can be the ID or an object reference to the HTML form
    &#x2F;&#x2F;     containing the input type=&quot;file&quot; elements.
    &#x2F;&#x2F; upload: Set this property to &quot;true&quot; to indicate this is a file
    &#x2F;&#x2F;         upload transaction.
    var cfg = {
        method: &#x27;POST&#x27;,
        form: {
            id: formObject,
            upload: true
        }
    };

    &#x2F;&#x2F; Define a function to handle the start of a transaction
    function start(id, args) {
      var id = id; &#x2F;&#x2F; Transaction ID.
      var args = args.foo; &#x2F;&#x2F; &#x27;bar&#x27;
    }

    &#x2F;&#x2F; Define a function to handle the response data.
    function complete(id, o, args) {
      var id = id; &#x2F;&#x2F; Transaction ID.
      var data = o.responseText; &#x2F;&#x2F; Response data.
      var args = args[1]; &#x2F;&#x2F; &#x27;ipsum&#x27;.
    };

    &#x2F;&#x2F; Subscribe to event &quot;io:start&quot;, and pass an object
    &#x2F;&#x2F; as an argument to the event handler &quot;start&quot;.
    Y.on(&#x27;io:start&#x27;, start, Y, { &#x27;foo&#x27;:&#x27;bar&#x27; });

    &#x2F;&#x2F; Subscribe to event &quot;io:complete&quot;, and pass an array
    &#x2F;&#x2F; as an argument to the event handler &quot;complete&quot;.
    Y.on(&#x27;io:complete&#x27;, complete, Y, [&#x27;lorem&#x27;, &#x27;ipsum&#x27;]);

    &#x2F;&#x2F; Start the transaction.
    var request = Y.io(uri, cfg);
});</pre>


<p>
When performing a file upload, a subset of global and transaction events will be fired.  Specifically, these are:
</p>
<ul>
    <li>Start</li>
    <li>Complete</li>
    <li>End</li>
</ul>
<p>Success and Failure events are not processed and fired because the iframe transport does not provide access to the HTTP status and response headers, to reliably determine those conditions.</p>

<h3 id="setting-http-headers">Setting HTTP Headers</h3>
<p>
IO can be configure to send default, user-defined HTTP Headers for all transactions, in addition to any headers defined in the configuration object.  Headers can be set or removed as needed.  The following example shows how to set and how to delete default headers in IO:
</p>

<pre class="code prettyprint">YUI().use(&quot;io-base&quot;, function(Y) {

    &#x2F;&#x2F; Set a new default HTTP header.
    Y.io.header(&#x27;Content-Type&#x27;, &#x27;application&#x2F;json&#x27;);

    &#x2F;&#x2F; To remove an existing header, use the same method, but omit the value.
    Y.io.header(&#x27;Content-Type&#x27;);
});</pre>


<p>Custom HTTP headers may or may not be sent in cross-domain requests.  This is may be due to limitations of the transport, or specific "Access-Control" headers requirement.</p>

<h3 id="queue">Queue</h3>
<p>
IO's queue module provides FIFO transaction response while keeping each transaction asynchronous and non-blocking.  Specifically, transactions are handled -- by global or transaction event handlers  -- in the order they are sent, regardless of actual server response order.  Transactions can be promoted to the front of the queue, or they can be purged from the queue, as well.
</p>
<table>
    <thead>
        <tr><th>Field</th><th>Description</th></tr>
    </thead>
    <tbody>
    <tr>
      <td><strong>queue(uri, configuration)</strong></td>
      <td>Method signature is identical to io, but returns the id of the transaction.</td>
    </tr>
    <tr>
      <td><strong>queue.start()</strong></td>
      <td>Activates the queue, and begins processing transactions in the queue.  This is the default state of the queue.</td>
    </tr>
    <tr>

      <td><strong>queue.stop()</strong></td>
      <td>Deactivates the queue.  Transactions sent to queue() will be stored until the queue is re-started.</td>
    </tr>
      <td><strong>queue.promote(id)</strong></td>
      <td>Moves the specified transaction stored in the queue to the head of the queue.</td>
    </tr>
    <tr>
      <td><strong>queue.remove(id)</strong></td>
      <td>Deletes the specified transaction stored in the queue.</td>
    </tr>
    </tbody>
</table>

<pre class="code prettyprint">&#x2F;&#x2F; Create a YUI instance using the io queue sub-module.
YUI().use(&quot;io-queue&quot;, function(Y) {

    &#x2F;&#x2F; Stop the queue so transactions can be stored.
    Y.io.queue.stop();

    &#x2F;&#x2F; Send four transactions into the queue. Each response will arrive
    &#x2F;&#x2F; in synchronous order.
    var task0 = Y.io.queue(uri);
    var task1 = Y.io.queue(uri);
    var task2 = Y.io.queue(uri);
    var task3 = Y.io.queue(uri);

    &#x2F;&#x2F; Promote task2 to the top of the queue.
    Y.io.queue.promote(task2);

    &#x2F;&#x2F; Remove task3 from the queue.
    Y.io.queue.remove(task3);

    &#x2F;&#x2F; Re-start the queue.
    &#x2F;&#x2F; Transactions are sent in the following order: task2, task0, task 1.
    &#x2F;&#x2F; Transaction callbacks, if provided, will be processed in the same
    &#x2F;&#x2F; sequence: task2, task0, task1, regardless of actual response order.
    Y.io.queue.start();
});</pre>


<h3 id="instantiating-io">Instantiating IO</h3>
<p>
As of 3.4.0, IO is instantiatiable.  An IO instance avails its public and private fields, allowing for customizations as needed.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create a new instance of IO.
var io = new IO();

&#x2F;&#x2F; Send a request using the new IO instance.
&#x2F;&#x2F; This is analogous to the static method
&#x2F;&#x2F; Y.io()
io.send(uri, configuration);</pre>


<p>
In addition to being instantiable, IO is now an EventTarget, and IO's global events can be configured at instantiation time.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; This simple example creates a new instance of IO and passes
&#x2F;&#x2F; Custom Event configurations that instructs IO to emit
&#x2F;&#x2F; Event Facades for all its events, and allow the events to 
&#x2F;&#x2F; bubble to other registered event targets, if any.
var io = new IO({
    emitFacade: true, &#x2F;&#x2F; Event handlers will receive an Event Facade.
    bubbles: true, &#x2F;&#x2F; Events will bubble to registered event targets.
});</pre>


<p>
If IO is configured to emit Event Facades, each event handler will receive the Event Facade as the argument.</p>
</p>

<pre class="code prettyprint">&#x2F;&#x2F; This is the event handler using Event Facades.
var configuration = {
    on: {
        complete: function(o) {
            &#x2F;*
             * o is the event facade, and contains the following fields:
             * - o.id is the transaction id.
             * - o.data is the XMLHttpRequest (or other transport) object.
             * - o.arguments is the user-defined arguments, if any.
             * - o.cfg is the configuration object used for this transaction.
             *
             * These fields are in addition to the Event Facade&#x27;s fields.
             *&#x2F;
        }
    }
};

&#x2F;&#x2F; For comparison, this is the regular event handler, when 
&#x2F;&#x2F; not emitting Event Facades as described in the previous
&#x2F;&#x2F; sections on &quot;The Response Object&quot; and &quot;Events.&quot;
var configuration = {
    on: {
        complete: function(id, xhr, arguments) {
            &#x2F;&#x2F; id is the transaction id.
            &#x2F;&#x2F; xhr is the XMLHttpRequest object.
            &#x2F;&#x2F; arguments is the user-defined arguments, if any.
        }
    }
};</pre>


<h2 id="security-bulletin">Security Bulletin</h2>
<p>A security vulnerability exists in the XDR transport <code>io.swf</code> when using the <code>io-xdr</code> sub-module to make cross-domain requests.  This vulnerability allows third-party sites to load <code>io.swf</code> from a remote domain and issue HTTP requests with the SWF's domain credentials.  Please examine the following use cases, and, if applicable to you, please follow the recommended actions to close this exploit.
</p>
<ul>
    <li> You currently host <code>io.swf</code> from YUI 3.1.0, 3.1.1, or 3.2.0pr1, and your application uses the io-xdr sub-module to make cross-domain requests.  Solution: replace the version of <code>io.swf</code> with <code>io.swf</code> from YUI 3.1.2.</li>
    <li> Your application uses the <code>io-xdr</code> sub-module from version YUI 3.1.0, 3.1.1, and you explicitly load <code>io.swf</code> from <code>http://yui.yahooapis.com/version/build/io.swf</code> (where <code>version</code> matches the affected YUI versions).  Solution: modify your application's <code>crossdomain.xml</code> so that <code>allow-access-from domain=</code> does <strong>not</strong> allow access from yui.yahooapis.com.  Download YUI 3.1.2 and deploy <code>io.swf</code> on your application's domain instead of loading it from yui.yahooapis.com.
    <li> Your application uses the <code>io-xdr</code> sub-module from version YUI 3.1.0, 3.1.1, and you explicitly load <code>io.swf</code> from a disparate domain, and you have a crossdomain policy file allowing access from the SWF's domain.  Solution: modify your application's crossdomain.xml so that <code>allow-access-from domain=</code> does <strong>not</strong> allow access from the domain serving <code>io.swf</code>.  Download YUI 3.1.2 and deploy <code>io.swf</code> on your application's domain instead of loading it from a remote domain.
    <li> If you use <code>io.swf</code> from YUI 3.0.0 you are not affected by this vulnerability.</li>
</ul>
<p>Beginning with YUI 3.1.2, <code>io.swf</code> will no longer be accessible from yui.yahooapis.com.  You will be required to host and serve <code>io.swf</code>, if you wish to employ it as an XDR transport.</p>

<h2 id="known-issues">Known Issues</h2>
<ul>
    <li>Multiple HTML Submit buttons, in an HTML form, are not supported at this time.</li>
</ul>
</div>
        </div>

        <div id="sidebar" class="yui3-u">
            
                <div id="toc" class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Table of Contents</h2>
                    </div>

                    <div class="bd">
                        <ul class="toc">
<li>
<a href="#getting-started">Getting Started</a>
</li>
<li>
<a href="#a-simple-transaction">A Simple Transaction</a>
</li>
<li>
<a href="#using-io">Using IO</a>
<ul class="toc">
<li>
<a href="#the-io-modules">The IO modules</a>
</li>
<li>
<a href="#the-configuration-object">The Configuration Object</a>
</li>
<li>
<a href="#the-response-object">The Response Object</a>
</li>
<li>
<a href="#events">Events</a>
</li>
<li>
<a href="#synchronous-transactions">Synchronous Transactions</a>
</li>
<li>
<a href="#cross-domain-transactions">Cross-Domain Transactions</a>
</li>
<li>
<a href="#serializing-html-form-as-data">Serializing HTML Form as Data</a>
</li>
<li>
<a href="#uploading-files-in-an-html-form">Uploading Files in an HTML Form</a>
</li>
<li>
<a href="#setting-http-headers">Setting HTTP Headers</a>
</li>
<li>
<a href="#queue">Queue</a>
</li>
<li>
<a href="#instantiating-io">Instantiating IO</a>
</li>
</ul>
</li>
<li>
<a href="#security-bulletin">Security Bulletin</a>
</li>
<li>
<a href="#known-issues">Known Issues</a>
</li>
</ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="Use IO to request data over HTTP.">
                                        <a href="get.html">HTTP GET to request data</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Use IO to request XML data from a remote web service.">
                                        <a href="weather.html">Request XML data from Yahoo! Weather</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources.">
                                        <a href="xdr.html">Request JSON using Yahoo! Pipes</a>
                                    </li>
                                
                            
                                
                            
                        </ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples That Use This Component</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                            
                                
                            
                                
                            
                                
                                    <li data-description="Shows how to create a simple plugin to retrieve content for the Overlay using the io utility.">
                                        <a href="../overlay/overlay-io-plugin.html">IO Plugin</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
